package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.domain.customer.CustomerListDO;
import com.qk.dto.customer.CustomerListDTO;
import com.qk.entity.Course;
import com.qk.entity.Customer;
import com.qk.management.mapper.CourseMapper;
import com.qk.management.mapper.CustomerMapper;
import com.qk.management.service.CustomerService;
import com.qk.vo.clue.CluePoolLitsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {
    @Autowired
    private CourseMapper courseMapper;

    //修改客户
    @Override
    public void updateCustomer(Customer customer) {
        this.baseMapper.updateById(customer);
    }


    //根据id查询客户
    @Override
    public Customer getCustomerById(Integer id) {
        return this.baseMapper.selectById(id);
    }


    //添加客户
    @Override
    public void addCustomer(Customer customer) {
        customer.setUpdateTime(LocalDateTime.now());
        customer.setCreateTime(LocalDateTime.now());
        this.baseMapper.insert(customer);
    }


    //客户列表查询
    @Override
    public PageResult<CustomerListDO> getCustomerList(CustomerListDTO dto) {
        IPage<Customer> pages = new Page<>(dto.getPage(),dto.getPageSize());
        IPage<Customer> customerIPage = this.baseMapper.selectPage(pages,
                Wrappers.lambdaQuery(Customer.class)
                        .eq(ObjectUtil.isNotEmpty(dto.getPhone()), Customer::getPhone, dto.getPhone())
                        .eq(ObjectUtil.isNotEmpty(dto.getName()), Customer::getName, dto.getName())
                        .eq(ObjectUtil.isNotEmpty(dto.getChannel()), Customer::getChannel, dto.getChannel())
                        .eq(ObjectUtil.isNotEmpty(dto.getSubject()), Customer::getSubject, dto.getSubject())
        );
        List<CustomerListDO> customerListDOList = new ArrayList<>();
        for (Customer customer : customerIPage.getRecords()) {
            CustomerListDO customerListDO = BeanUtil.copyProperties(customer, CustomerListDO.class);
            if (ObjectUtil.isNotEmpty(customer.getCourseId())){
                Course course = this.courseMapper.selectById(customer.getCourseId());
                if (ObjectUtil.isNotEmpty(course)) {
                    customerListDO.setCourseName(course.getName());
                }
            }
            customerListDOList.add(customerListDO);
        }
        return PageResult.<CustomerListDO>builder()
                .total((int) customerIPage.getTotal())
                .rows(customerListDOList)
                .build();
    }
}
